package com.good.child.widgets.addressPicker;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.good.child.R;

import java.util.List;

/**
 * Create on: 2024/2/28
 * by Mr.Zhu
 * Description: TODO
 */
public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.MyViewHolder> {

    private final int layoutResId;
    private final List<AddressBean> data;
    private OnItemClickListener onItemClickListener;

    public AddressAdapter(int layoutResId, @Nullable List<AddressBean> data) {
        this.layoutResId = layoutResId;
        this.data = data;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public AddressAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(this, LayoutInflater.from(viewGroup.getContext()).inflate(layoutResId, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AddressAdapter.MyViewHolder viewHolder, int i) {
        viewHolder.textView.setText(data.get(i).getAreaName());
        viewHolder.textView.setTextColor(data.get(i).isStatus() ? Color.parseColor("#ED4444") : Color.parseColor("#444444"));
    }


    public AddressBean getItem(int position) {
        return data.get(position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;

        public MyViewHolder(final AddressAdapter addressAdapter, @NonNull View itemView) {
            super(itemView);
//            itemView.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
            textView = itemView.findViewById(R.id.textview);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addressAdapter.onItemClickListener.onItemClick(v, getLayoutPosition());
                }
            });
        }
    }
}

